﻿#define _CRT_SECURE_NO_WARNINGS
// ↑ 将本行在代码放在第一行可以避免 scanf的报错问题（仅在VS编译器里）
//【初阶】45.1 栈和队列2
//【初阶】45.2 栈和队列2
//【初阶】45.3 栈和队列2


//【头文件包含】

#include"Queue.h"

//【结构体声明】
/*双队列栈*/typedef struct 
{
	Que q1;
	Que q2;
}MyStack;

//【函数声明】
void QueueTest1();

//【主函数】
int main()
{
	
	QueueTest1();
	return 0;
}

//【函数】

/*栈测试用例*/void QueueTest1()
{
	printf("****队列测试用例1开始********************\n");

	Que queue1;
	printf("初始化\n");
	QueueInit(&queue1);
	printf("插入数据1,2,3,4,5\n");
	QueuePush(&queue1, 1);
	QueuePush(&queue1, 2);
	QueuePush(&queue1, 3);
	QueuePush(&queue1, 4);
	QueuePush(&queue1, 5);
	QueuePrint(&queue1);
	printf("队列反向打印\n");
	QueuePrintRev(&queue1);
	printf("队列数据删除1次\n");
	QueuePop(&queue1);
	QueuePrint(&queue1);
	printf("队列队头数据提取\nFront = [%d]\n", QueueFront(&queue1));
	printf("队列队尾数据提取\n Back = [%d]\n", QueueBack(&queue1));
	printf("队列空判断\n");
	if (QueueEmpty(&queue1) == true)printf("Empty\n");
	else printf("NonEmpty\n");
	printf("队列大小计算\nSIZE = %d\n", QueueSize(&queue1));
	printf("队列数据删除4次\n");
	QueuePop(&queue1);
	QueuePop(&queue1);
	QueuePop(&queue1);
	QueuePop(&queue1);
	printf("队列空判断\n");
	if (QueueEmpty(&queue1) == true)printf("Empty\n");
	else printf("NonEmpty\n");
	printf("队列大小计算\nSIZE = %d\n", QueueSize(&queue1));
	printf("队列空间销毁\n");
	QueueDestroy(&queue1);

	printf("****队列测试用例1结束********************\n\n\n");

}


MyStack* mystackcreat()
{
	MyStack* st=(MyStack*)malloc(sizeof(MyStack));
	QueueInit(&st->q1);
	QueueInit(&(st->q2));
	return st;
}
void myStackPush(MyStack* obj, int x)
{
	if (!QueueEmpty(&obj->q1))
	{
		QueuePush(&obj->q1, x);
	}
	else
	{
		QueuePush(&obj->q2, x);
	}
}
int myStackPop(MyStack* obj)
{
	Que* emptyQ = &obj->q1;
	Que* nonemptyQ = &obj->q2;
	if (!QueueEmpty(&obj->q1))
	{
		emptyQ = &obj->q2;
		nonemptyQ =&obj->q1;
	}
	while (QueueSize(nonemptyQ)>1)
	{
		QueuePush(emptyQ, QueueFront(nonemptyQ));
		QueuePop(nonemptyQ);
	}
	int ret = QueueFront(nonemptyQ);
	QueuePop(nonemptyQ);
	return ret;
}
int myStackTop(MyStack* obj) 
{
	if (!QueueEmpty(&obj->q1))
	{
		return QueueBack(&obj->q1);
	}
	else
	{
		return QueueBack(&obj->q2);
	}
}
bool myStackEmpty(MyStack* obj) 
{
	return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
}
void myStackFree(MyStack* obj)
{
	QueueDestroy(&obj->q1);
	QueueDestroy(&obj->q2);
	free(obj);
}

//【笔记】
//【初阶】45.1 栈和队列2
	//队列：只允许在一段插入数据另一端删除数据
	//队列先进先出，入队列是队尾，出队列是队头 


